package com.zeltov.hibernate;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.zeltov.dao.ArtDataDao;
import com.zeltov.eo.ArtData;

@Repository("artDataDao")
public class ArtDataDaoHibernate extends GenericDaoHibernate<ArtData> implements ArtDataDao {

    public ArtDataDaoHibernate() {
        super(ArtData.class);
    }

    private static final String STORAGE_FORMAT = "STORAGE_FORMAT";
    private static final String GALLERY_FORMAT = "GALLERY_FORMAT";
    private static final String THUMBNAIL_FORMAT = "THUMBNAIL_FORMAT";
/*
    // gets (loading proxy) appropriate image format from artEntity (such as thumbnail, etc.)
    public ArtData getArtDataFromEntity(ArtEntity entity, String imageFormat) throws DataAccessException {
        if (imageFormat.equalsIgnoreCase(STORAGE_FORMAT)) {
            if (entity.getStoragePicture() instanceof HibernateProxy)
                this.getHibernateTemplate().initialize(entity.getStoragePicture());
            return entity.getStoragePicture();
        } else if (imageFormat.equalsIgnoreCase(GALLERY_FORMAT)) {
            if (entity.getGalleryPicture() instanceof HibernateProxy)
                this.getHibernateTemplate().initialize(entity.getGalleryPicture());
            return entity.getStoragePicture();
        } else if (imageFormat.equalsIgnoreCase(THUMBNAIL_FORMAT)) {
            if (entity.getThumbnailPicture() instanceof HibernateProxy)
                this.getHibernateTemplate().initialize(entity.getThumbnailPicture());
            return entity.getThumbnailPicture();
        }
        return null;
    }
*/
	@Override
	public List<ArtData> getAllArtByQuery() throws DataAccessException {
//		this.getHibernateTemplate().getSessionFactory().getCurrentSession()
		SessionFactory sessionFactory = this.getSessionFactory();
		//Query query = sessionFactory.getCurrentSession().getNamedQuery("findArtQuery");
		//NOT SURE HOW TO IMPLEMENT THIS IF RUNNING OUTSIDE TRANSACTION MANAGER
		//ONE SUGGESTION USING sessionFactory.openSession and manualy manage connection, etc...
		return this.getHibernateTemplate().findByNamedQuery("findArtQuery");
//		query.setString(arg0, arg1)
		//return query.list();
//		return null;
	}	
}
